#!/bin/bash

#=======================================================================
# Genlog
# File ID: 20281518-f989-11dd-98ac-000475e441b9
#
# Run all tests, update files in log/ and display the results.
#
# License: GNU General Public License version 2 or later.
#=======================================================================

[ -z "$1" ] && { echo "Syntax: $0 testname [options] [testname [...]]" >&2; exit 1; }

mkdir -p log
maxlen=0
retval=0
tot_ok=0
tot_not_ok=0
opt_str=

while printf "%s" "$1" | grep -q ^-; do
    opt_str="$opt_str $1"
    shift
done

for gf in "$@"; do
    currlen=`echo -n $gf | perl -pe 's/\.t$//' | wc -c`
    [ $currlen -gt $maxlen ] && maxlen=$currlen
done

for gf in "$@"; do
    testname=$gf
    testname=`echo -n $testname | perl -pe 's/\.t$//'`
    printf "%${maxlen}s.t: " $testname
    ./$testname.t $opt_str 2>&1 | remove_perltestnumbers >log/$testname.log
    not_ok_count=$(grep "^not ok -" log/$testname.log | wc -l | tr -d ' ')
    if [ $not_ok_count -gt 0 ]; then
        not_ok_str=", $not_ok_count not ok"
        retval=1
    else
        unset not_ok_str
    fi
    ok_count=$(grep "^ok -" log/$testname.log | wc -l)
    printf "%3u ok%s\\n" $ok_count "$not_ok_str"
    tot_ok=$(($tot_ok + $ok_count))
    tot_not_ok=$(($tot_not_ok + $not_ok_count))
    grep -q "^not ok -" log/$testname.log && {
        echo
        grep "^not ok -" log/$testname.log
        echo
    }
done
echo
printf "  %${maxlen}s: %u ok, %u not ok\n" "total" $tot_ok $tot_not_ok

exit $retval
